<?php


namespace App\Http\Controllers\Mini;


use App\Http\Controllers\Controller;
use App\Models\CircleLike;
use App\Models\Comment;
use App\Models\Label;
use Illuminate\Http\Request;
use \App\Models\Circle;

class CircleController extends Controller
{

    private $user_id;

    public function __construct()
    {
        $id = auth('user')->id();

        $this->user_id = $id;
    }

    //邻里圈内容列表
    public function list(Request $request)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            $this->error('请先登录');
        }

        $limit = $request->get('limit', 20);
        $type = $request->get('type');

        $where = [];

        if ($type == 'my') {
            $where[] = ['user_id', '=', $user_id];
        }

        //获取用户已经点赞过的动态
        $like_data = CircleLike::query()->where('user_id', $user_id)->value('circle_ids') ?? '[]';
        $like_data = json_decode($like_data, true);
        /**
         * 一对多关联，label_ids结构是json格式的字符串
         * FIXME：用foreach循环处理，也许会有更好的方法
         */
        $data = Circle::query()->with(['user:id,nickname,avatar', 'comment'])->select('id', 'user_id', 'label_ids', 'star', 'content', 'images', 'created_at')->where($where)->paginate($limit);
        foreach ($data->items() as &$item) {
            $item['label_ids'] = json_decode($item['label_ids'], true);
            $item['label'] = Label::query()->whereIn('id', $item['label_ids'])->pluck('title');
            $item['comment_count'] = count($item['comment']);
            $images = json_decode($item['images'], true);


            //是否点赞过
            $item['is_like'] = false;
            if (in_array($item['id'], $like_data)) {
                $item['is_like'] = true;
            }

            //处理图片路径 返回给前端绝对路径
            foreach ($images as $key => $value) {
                $images[$key] = asset($value);
            }
            $item['images'] = $images;
            unset($item['label_ids']);
        }

        return $this->success($data);
    }

    //发布邻里圈内容
    public function add(Request $request)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }


        $label_id = $request->post('label_id', '');
        $content = $request->post('content');
        $images = $request->post('images', '');

        $label_id = array_filter(explode(',', $label_id), function ($item) {
            return is_numeric($item);
        });

        $images = array_filter(explode(',', $images), function ($item) {
            return !empty($item);
        });


        $images = json_encode($images) ?? '[]';
        $label_id = json_encode($label_id);

        if (!$content) {
            return $this->error('请输入内容');
        }

        $data = [
            'label_ids' => $label_id,
            'images' => $images,
            'content' => $content,
            'user_id' => $user_id
        ];

        Circle::query()->create($data);

        return $this->success([], '发布成功');
    }

    //邻里圈内容详情
    public function show($id)
    {
        $user_id = $this->user_id;

        $data = Circle::query()->select('id', 'user_id', 'content', 'label_ids', 'images', 'star', 'created_at')->find($id);

        if ($data) {
            //如果用户登录了
            if ($user_id) {
                $data['is_like'] = false;
                //获取用户已经点赞过的动态
                $like_data = CircleLike::query()->where('user_id', $user_id)->value('circle_ids') ?? '[]';
                $like_data = json_decode($like_data, true);
                if (in_array($data['id'], $like_data)) {
                    $data['is_like'] = true;
                }
            }

            $data['label_ids'] = json_decode($data['label_ids'], true);
            $data['label'] = Label::query()->whereIn('id', $data['label_ids'])->pluck('title');
            //评论数量
            $data['comment_count'] = Comment::query()->where('circle_id', $data['id'])->count();
            unset($data['label_ids']);

            return $this->success($data, '请求成功');
        }

        return $this->error('内容不存在');
    }

    //删除我发布的邻里圈内容
    public function delete($id)
    {
        // 获取当前用户user_id
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        //执行删除
        Circle::query()->where('id', $id)->delete();

        //同时删除底下的评论
        Comment::query()->where('circle_id', $id)->delete();

        return $this->success([], '删除成功');
    }
}
